//
//  TodayDetailExplainView.swift
//  MoyoungHome
//
//  Created by 易先生 on 2022/9/20.
//

import UIKit
import MYModule

open class ExPlainView: View {
    
    open override func makeUI() {
        super.makeUI()
        
        self.addSubview(borderView)
        borderView.addSubview(bgView)
        borderView.addSubview(titleLabel)
        borderView.addSubview(explainLabel)
        
        borderView.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(20)
            make.leading.equalToSuperview().offset(20)
            make.trailing.equalToSuperview().offset(-20)
            make.bottom.equalToSuperview().offset(-20)
        }

        bgView.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(0.5)
            make.leading.equalToSuperview().offset(0.5)
            make.trailing.equalToSuperview().offset(-0.5)
            make.bottom.equalToSuperview().offset(-0.5)
        }

        titleLabel.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(15)
            make.leading.equalToSuperview().offset(15)
            make.trailing.equalToSuperview().offset(-15)
        }

        explainLabel.snp.makeConstraints { make in
            make.top.equalTo(titleLabel.snp.bottom).offset(10)
            make.leading.equalToSuperview().offset(15)
            make.trailing.equalToSuperview().offset(-15)
            make.bottom.equalToSuperview().offset(-15)
        }
    }
    
    public func updateData(textColor: UIColor,
                           title: String,
                           explain:String,
                           explainTextColor: UIColor,
                           bgColor: UIColor) {
        
        let explainTextColor = explainTextColor
        titleLabel.text = title
        borderView.layer.borderColor = textColor.cgColor
        bgView.backgroundColor = bgColor
        titleLabel.textColor = textColor
        explainLabel.textColor = explainTextColor
        
        let attributedText = explain.toAttributed
            .foregroundColor(explainTextColor)
            .font(explainLabel.font)
            .paragraph(setup: {
                
                $0.lineSpacing = 5
                $0.firstLineHeadIndent = 0
                
            }).rawValue
        
        explainLabel.attributedText = attributedText
        
    }

    
    private lazy var borderView: View = {
        let borderView = View()
        borderView.backgroundColor = .clear
        borderView.layer.cornerRadius = 14
        borderView.layer.borderWidth = 1
        borderView.clipsToBounds = true
        return borderView
    }()

    private lazy var bgView: View = {
        let bgView = View()
        bgView.alpha = 0.1
        return bgView
    }()

    private lazy var titleLabel: Label = {
        let titleLabel = Label()
        titleLabel.numberOfLines = 0
        titleLabel.font = Fonts.boldSystemFont(ofSize: 14)
        return titleLabel
    }()

    private lazy var iconImageView: ImageView = {
        let iconImageView = ImageView()
        return iconImageView
    }()

    private lazy var explainLabel: Label = {
        let explainLabel = Label()
        explainLabel.numberOfLines = 0
        explainLabel.font = Fonts.systemFont(ofSize: 12)
        return explainLabel
    }()

}
